<template>
  <div class="">
    <!-- 任务状态 -->
    <div class="inStatus">
      <div :class=" playStatusObj ? playStatus.is_pass ? 'taskStatus' : 'taskNuStatus' : 'taskNuStatus'">
      </div>
      {{ playStatusObj ? playStatus.is_pass ? '已完成' : '学习中' : '未完成' }}
      <span style="font-size: 12px;color: #9e9897;margin-left: 20px"></span>
    </div>
    <!-- end -->
    <div class="thisVideo">
      <div class=''>
        <video-player @ready="playerReadied" @pause="onpause($event)" @timeupdate="onPlayerPlaying($event)"
                      class="video-player vjs-custom-skin"
                      ref="videoPlayer"
                      :playsinline="true"
                      :options="playerOptions">
        </video-player>
      </div>
    </div>
    <!-- 上下按钮 -->
    <div class="nextOrLast">
      <div @click="perChapter" class="lastInit">
        上一集
      </div>
      <div @click="nexChapter" class="nextInit">
        下一集
      </div>
    </div>
    <!-- end -->
  </div>
</template>

<script>
let _this;
export default {
  props: ['src', 'playStatus', 'play_time'],
  data() {
    return {
      playStatusObj: null, //当前播放是否已完成
      duration: 0, //总时长
      currenTimer: 0, //当前播放时间
      classInfo: null, //课程信息
      playerOptions: {
        currentTime: 0,
        playbackRates: [0.5, 1.0, 1.5, 2.0], // 可选的播放速度
        autoplay: false, // 如果为true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 是否视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '4:3', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [{
          type: "video/mp4", // 类型
          src: 'http://vjs.zencdn.net/v/oceans.mp4' // url地址
        }],
        poster: '', // 封面地址
        notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          progressControl: true, //进度条
          remainingTimeDisplay: false, // 是否显示剩余时间功能
          fullscreenToggle: true // 是否显示全屏按钮
        },
      },
    }
  },
  watch: {
    src: {
      handler(val) {
        this.playerOptions.sources[0].src = val;
      },
      deep: true,
      immediate: true
    },
    play_time: {
      handler(val) {
        this.playerOptions.currentTime = val;
      },
      deep: true,
      immediate: true
    },
    playStatus: {
      // 判断是否有学习记录 有 在判断是否播放完成  如果播放完成则可以拖动进度条,开启倍速
      handler(val) {
        this.playStatusObj = val;
        this.classInfo = JSON.parse(sessionStorage.getItem('classInfoV'));
        if (val){
          if (val.is_pass) {
            this.playerOptions.playbackRates = [0.5, 1.0, 1.5, 2.0]
            this.playerOptions.controlBar.progressControl = true;
            return
          }
          if (!this.classInfo.is_speed){
            this.playerOptions.playbackRates = [1]
          }
          if (!this.classInfo.is_drat){
            this.playerOptions.controlBar.progressControl = false;
          }
        }else{
          if (!this.classInfo.is_speed){
            this.playerOptions.playbackRates = [1]
          }
          if (!this.classInfo.is_drat){
            this.playerOptions.controlBar.progressControl = false;
          }
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {

    //播放暂停
    playPause: function() {
      this.$emit("onpause", {currenTimer: this.currenTimer, duration: this.duration})
      this.$refs.videoPlayer.player.pause();
    },
    playerReadied: function(player) {
      player.currentTime(this.playerOptions.currentTime)
    },
    onPlayerPlaying: function(e) {
      let duration = parseInt(e.cache_.duration);//总时长
      let currentTime = parseInt(e.cache_.currentTime);
      this.currenTimer = currentTime;
      this.duration = duration;
      let hundred = 0;
      if (currentTime > 3) {
        hundred = (currentTime / duration).toFixed(2) * 100;
        if (hundred >= this.classInfo.progress) {
          this.$emit('onPlayerPlaying', {playIng: hundred, currenTimer: currentTime})
        }
      }
    },
    onpause: function() {
      this.$emit("onpause", {currenTimer: this.currenTimer, duration: this.duration})
    },
    perChapter: function() {
      this.saveCurrentPlayTime()
      this.$emit("perChapter", {currenTimer: this.currenTimer, duration: this.duration})
    },
    nexChapter: function() {
      this.saveCurrentPlayTime()
      this.$emit("nexChapter", {currenTimer: this.currenTimer, duration: this.duration})
    },
    saveCurrentPlayTime: function() {
      let dataList = JSON.parse(sessionStorage.getItem("chapterList"));
      let cruurent = JSON.parse(sessionStorage.getItem('chapterIndex'))
      let chapterIndex = cruurent.chapterIndex;
      let cindex = cruurent.index;
      let data = dataList[cindex].children[chapterIndex].VideoRecord;
      if (data) {
        dataList[cindex].children[chapterIndex].VideoRecord = {
          id: data.id,
          chapter_name: data.chapter_name,
          is_pass: data.is_pass,
          play_time: this.currenTimer,
          chapter_id: data.chapter_id
        }
      } else {
        dataList[cindex].children[chapterIndex].VideoRecord = {play_time: this.currenTimer}
      }
      sessionStorage.setItem("chapterList", JSON.stringify(dataList))

    }
  },
  created() {
    _this = this;
    this.classInfo = JSON.parse(sessionStorage.getItem('classInfoV'));

  },

}
</script>

<style>

.lastInit {
  font-size: 18px;
  padding: 6px 20px;
  border: solid 1px #a898ff;
  color: #a898ff;
  border-radius: 30px;
  margin-right: 32px;
  cursor: pointer;
}

.nextInit {
  font-size: 18px;
  font-size: 18px;
  padding: 6px 20px;
  background-image: linear-gradient(to bottom right, #67a1ff, #616dff);
  color: #FFFFFF;
  cursor: pointer;
  border-radius: 30px;
}

.nextOrLast {
  margin-top: 16px;
  margin-bottom: 24px;
  display: flex;
  justify-content: flex-end;
}

.taskStatus {
  width: 20px;
  height: 20px;
  background-color: #7a9933;
  border-radius: 50%;
  margin-right: 6px;
}

.taskNuStatus {
  width: 20px;
  height: 20px;
  background-color: #fed55f;
  border-radius: 50%;
  margin-right: 6px;
}

.inStatus {
  width: 675px;
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.myVideo {
  margin-top: -16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 65px;
  background-color: #FFFFFF;
}
</style>
